package com.hb.trade.api.push.task.wincandidatebulletinphase;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.project.deal.dto.BidCandidateSupplierDTO;
import com.hb.core.pojo.project.opening.entity.OpeningBidTender;
import com.hb.trade.client.center.OrgClient;
import lombok.Data;
import org.fzzn.core.model.msg.Message;
import com.hb.trade.client.project.BidOpeningHallClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe:
 *
 * @author zhongdl
 * @date 2022/02/23
 */
@Data
public class SectionsWinCandidate {

    /**
     * 标段（包）编号
     */
    @JsonProperty("BID_SECTION_CODE")
    private String bidSectionCode;
    /**
     * 标段（包）编号
     */
    @JsonProperty("WIN_CANDIDATES")
    private WinCandidateS winCandidates;


    public SectionsWinCandidate(String bidSectionCode, List<BidCandidateSupplierDTO> bidCandidateSupplierDTOList) {
        if (CollUtil.isEmpty(bidCandidateSupplierDTOList)){
            return;
        }
        WinCandidateS winCandidateS = new WinCandidateS();
        List<WinCandidate> winCandidateList = CollUtil.list(false);
        OrgClient orgClient = SpringUtil.getBean(OrgClient.class);
        int ranking = 0;
        for (BidCandidateSupplierDTO bidCandidateSupplierDTO : bidCandidateSupplierDTOList) {
            WinCandidate winCandidate = new WinCandidate();
            if(null == bidCandidateSupplierDTO.getOrgId()){
                // 如果公司是空的说明手动添加的，所以推送中国服务平台数据需要推送默认的
                winCandidate = pushDefaultInfo(ranking, winCandidate, bidCandidateSupplierDTO);
                winCandidateList.add(winCandidate);
                continue;
            }
            Message<OrgDTO> org = orgClient.getOrgById(bidCandidateSupplierDTO.getOrgId());
            if (org.isFail() || org.getData() == null){
                return;
            }
            //查询投标人解密内容
            BidOpeningHallClient bidOpeningHallClient = SpringUtil.getBean(BidOpeningHallClient.class);
            OpeningBidTender openingBidTender = new OpeningBidTender();
            openingBidTender.setSectionId(bidCandidateSupplierDTO.getSectionId());
            openingBidTender.setOrgId(bidCandidateSupplierDTO.getOrgId());
            Message<OpeningBidTender> openingBidTenderMsg = bidOpeningHallClient.getOpeningBidTenderBySection(openingBidTender);
            if (openingBidTenderMsg.isFail() || openingBidTenderMsg.getData() == null){
                return;
            }
            OpeningBidTender bidTender = openingBidTenderMsg.getData();
            String decryptContent = bidTender.getDecryptContent();
            JSONObject jsonObject = JSONUtil.parseObj(decryptContent);
            //设置标段
            winCandidate.setWinCandidateCodeType("05");
            winCandidate.setWinCandidateCode(org.getData().getCreditCode());
            winCandidate.setWinCandidateName(bidCandidateSupplierDTO.getOrgName());
            winCandidate.setWinCandidateOrder((++ranking) + "");
            BidResult bidResult = new BidResult(jsonObject);
            winCandidate.setBidResult(bidResult);
            //质量
            winCandidate.setQuality(StrUtil.isEmpty(jsonObject.getStr("质量")) ?  "合格" : jsonObject.getStr("质量"));
            //评标情况
            winCandidate.setEvaluationStatus("是");
            //中标候选人响应招标文件的资格能力条件
            winCandidate.setWinCandidateQualification("/");
            // 工期/交货期/服务期
            winCandidate.setTimeLimit(StrUtil.isEmpty(jsonObject.getStr("工期（天）")) ? "0" : jsonObject.getStr("工期（天）"));
            //项目负责人姓名
            winCandidate.setPmName(StrUtil.isEmpty(jsonObject.getStr("企业名称")) ? "/" : jsonObject.getStr("企业名称"));
            //项目负责人姓名
            winCandidate.setPmCode("/");
            //项目负责人相关证书和编号
            winCandidate.setPmCertificates(winCandidate.getQuality());

            winCandidateList.add(winCandidate);


        }
        setBidSectionCode(bidSectionCode);
        winCandidateS.setWinCandidateList(winCandidateList);
        setWinCandidates(winCandidateS);
    }


    /**
     * 推送默认数据（只针对手动添加的中标候选人）
     *
     * @author tian
     * @date 2022-6-27 15:15
     * @param ranking
     * @param winCandidate
     * @param bidCandidateSupplierDTO
     */
    public WinCandidate pushDefaultInfo(int ranking, WinCandidate winCandidate, BidCandidateSupplierDTO bidCandidateSupplierDTO){
        //设置标段
        winCandidate.setWinCandidateCodeType("05");
        winCandidate.setWinCandidateCode(bidCandidateSupplierDTO.getCreditCode());
        winCandidate.setWinCandidateName(bidCandidateSupplierDTO.getOrgName());
        winCandidate.setWinCandidateOrder((++ranking) + "");

        JSONObject jsonObject = JSONUtil.parseObj("{\"无开标内容\":\"暂无\"}");
        BidResult bidResult = new BidResult(jsonObject);
        winCandidate.setBidResult(bidResult);
        //质量
        winCandidate.setQuality("/");
        //评标情况
        winCandidate.setEvaluationStatus("/");
        //中标候选人响应招标文件的资格能力条件
        winCandidate.setWinCandidateQualification("/");
        // 工期/交货期/服务期
        winCandidate.setTimeLimit("0");
        //项目负责人姓名
        winCandidate.setPmName("/");
        //项目负责人姓名
        winCandidate.setPmCode("/");
        //项目负责人相关证书和编号
        winCandidate.setPmCertificates(winCandidate.getQuality());

        return winCandidate;
    }
}
